<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <div class="login_form">
          <el-form :model="login_data" ref="login_ref" :rules="login_rules">
            <el-form-item prop="lphone">
              <el-input v-model="login_data.lphone" type="number" placeholder="请输入手机号" :prefix-icon="Iphone"
                clearable />
            </el-form-item>
            <el-form-item style="margin-bottom: 5px;" prop="lpass">
              <el-input v-model="login_data.lpass" type="password" placeholder="请输入密码" :prefix-icon="Lock" clearable
                show-password /> </el-form-item>
          </el-form>

          <el-button style="margin-top: 20px; width:100%" type="primary" @click="login_btn(login_ref)"
            :icon="User">登录</el-button>

        </div>
      </el-col>
    </el-row>

  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { Iphone, Lock, User } from '@element-plus/icons-vue'
import loginStore from '@/store/login/index.js'
const $loginStore = loginStore()
import { useRouter, useRoute } from 'vue-router'
const $useRouter = useRouter()
const $useRoute = useRoute()
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time.js'

//#region 登陆
const login_data = reactive({
  lphone: '18010588110',
  lpass: '123456',
})

const login_btn = (login_ref) => {
  if (!login_ref) return
  login_ref.validate(async (valid, fields) => {
    if (valid) {
      try {
        await $loginStore.userLogin(login_data)
        if ($useRoute.query.redirect) {

          $useRouter.push($useRoute.query.redirect)
        } else {

          $useRouter.push('/charter_consultation_list')
        }


        ElNotification({
          type: 'success',
          message: '欢迎回来',
          title: `HI,${getTime()}好`,
        })
      } catch (error) {
        ElNotification({
          type: "error",
          message: error.message,
        })
      }

    } else {
      console.log('登录表单验证不通过!', fields)
    }
  })

}

const login_ref = ref()

const login_rules = reactive({
  lphone: [
    { required: true, message: '请输入手机号', trigger: 'change' },
    { min: 11, max: 11, message: '收输入11位数字的手机号', trigger: 'blur' },
  ],
  lpass: [
    { required: true, message: '请输入密码', trigger: 'change' },
  ],
})

//#endregion


</script>

<style scoped lang='scss'>
.login_container {
  width: 100%;
  height: 100vh;
  background-image: url('@/assets/images/login-bg.jpg');
  background-size: cover;

  .login_form {
    position: relative;
    width: 45%;
    top: 35vh;
    left: 5vw;
    padding: 40px 40px 20px 40px;
    border-radius: 10px;
    background-color: #fff;
  }
}
</style>
